/**
 * @class   MediaDirectorImpl
 * @brief   
 *
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: MediaDirectorImpl.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Services_MediaDirectorImpl_H__
 #define __WOSH_Services_MediaDirectorImpl_H__

 #include <core/BundleGenericWorker.h>
 #include <core/Map.h>
 #include <core/Table.h>
 #include <core/Notification.h>
 #include <core/MessageContent.h>
 #include <core/InterfacesProvider.h>
 #include <core/MessageQueue.h>

 #include <core/Communicator.h>
 #include <core/Interpreter.h>
 #include <interfaces/services/CommunicatorService.h>
 #include <interfaces/services/InterpreterService.h>
 #include <interfaces/services/BuildingManager.h>

 #include <interfaces/devices/PlayerAudio.h>
 #include <framework/building/AudioBox.h>
 #include <framework/entertainment/MediaZone.h>
 #include <framework/entertainment/MediaTrack.h>
 #include <framework/entertainment/MediaList.h>

 #include "IMediaDirectorListener.h"


using namespace wosh;
using namespace wosh::building;
using namespace wosh::entertainment;


class MediaDirectorImpl : public BundleGenericWorker
 {
	public:
		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 * @param busDevices [in] the connector to Device Bus
		 */
		MediaDirectorImpl( BundleGeneric& bundle, BusConnector& busDevices );
		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~MediaDirectorImpl();


	public:
		WRESULT copyMediaListToZone( const string& playlist, const string& targetZone, const string& sourceZone );
		WRESULT synchZoneAgainstPlayer( const string& playlist, const string& targetZone );


	public:
		WRESULT copyMediaListToZone_( const MediaList* list, MediaZone* targetZone, MediaZone* sourceZone );

		WRESULT synchZoneAgainstPlayer( const MediaList* playlist, MediaZone* zone );
		WRESULT synchZoneFromPlayerRequest( MediaZone* zone );
		WRESULT updatedMediaListByDevice_( const MediaList* playlist, MediaZone* zone );





		void evalMessageIncoming( const Message& message );

void evalDeviceMessage_( MediaZone* zone, const Message& message );

void controlDevice( MessageContent* content, const URI& device_uri );

	public:

		MediaZone* getZoneByDeviceUri_( const URI& device_uri );

		MediaZone* getPlayerOfLocation_( const string& locationUri );
//		MediaZone* getPlayerByBuildingObj_( const string& bobjUri );

		MediaZone* getNearestMediaZone_( MediaZone* zone, bool playing_flag );

	public:

		WRESULT synchZones( const string& from_zone, const string& to_zone, bool move );

		WRESULT restoreZone( const string& zone );


//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:

		void setListener( IMediaDirectorListener* list )	{ this->listener = list; }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		const MapT<string, MediaZone*>& getZones() const		{ return this->zones; }
		MapT<string, MediaZone*>& getZones()					{ return this->zones; }

		const MapT<string, MediaList*>& getPlaylists() const	{ return this->playlists; }
		MapT<string, MediaList*>& getPlaylists()				{ return this->playlists; }

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		void evalMessage( Message* message );

	protected:

		WRESULT execCommand( MediaZone* player, MediaZone::PLAYER_COMMAND command, int command_value );

		WRESULT synchZones_( MediaZone* playerSrc, MediaZone* playerDest, bool move );
		WRESULT restoreZone_( MediaZone* player );

		void editZoneRanking_( const string& zonename, bool more_or_less );

	protected:



		/**
		 * @brief   
		 * @see     running
		 * @see     
		 */
		virtual void runThread();

//@}

		MapT<string, MediaZone*> zones;		///< name, object
		int timeoutEval;
		long zoneRanking;

		MapT<string, MediaList*> playlists;	///< name, object

		MessageQueue msgQueue;				///< messages' queue

		BusConnector& BusDevices;

		IMediaDirectorListener* listener;	///< callbacks


}; // class def


#endif //__WOSH_Services_MediaDirectorImpl_H__
